package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharShrineList;
import org.gdstash.character.GDCharUID;
import org.gdstash.db.DBShrine;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractShrinePane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharShrineAct6Pane extends GDAbstractShrinePane {
    private JCheckBox cbShatteredRealms;
    private JCheckBox cbTempleOfOsyr;
    private JCheckBox cbKorvanOasis;
    private JCheckBox cbCairanDocks;
    private JCheckBox cbSandblownRuins;
    private JCheckBox cbSanctuaryOfHorran;
    private JCheckBox cbRuinsOfAbyd;
    private JCheckBox cbValleyOfTheChosen;
    private JCheckBox cbSunwardSpire;
    private JCheckBox cbTombOfTheEldritchSun;
    private JCheckBox cbTombOfTheHeretic;
    private JCheckBox cbSecretQuest;
    private int difficulty;
    private boolean changed;

    public GDCharShrineAct6Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbShatteredRealms).addComponent(this.cbTempleOfOsyr).addComponent(this.cbKorvanOasis).addComponent(this.cbCairanDocks).addComponent(this.cbSandblownRuins).addComponent(this.cbSanctuaryOfHorran).addComponent(this.cbRuinsOfAbyd).addComponent(this.cbValleyOfTheChosen).addComponent(this.cbSunwardSpire).addComponent(this.cbTombOfTheEldritchSun).addComponent(this.cbTombOfTheHeretic).addComponent(this.cbSecretQuest));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbShatteredRealms)).addGroup(layout.createParallelGroup().addComponent(this.cbTempleOfOsyr)).addGroup(layout.createParallelGroup().addComponent(this.cbKorvanOasis)).addGroup(layout.createParallelGroup().addComponent(this.cbCairanDocks)).addGroup(layout.createParallelGroup().addComponent(this.cbSandblownRuins)).addGroup(layout.createParallelGroup().addComponent(this.cbSanctuaryOfHorran)).addGroup(layout.createParallelGroup().addComponent(this.cbRuinsOfAbyd)).addGroup(layout.createParallelGroup().addComponent(this.cbValleyOfTheChosen)).addGroup(layout.createParallelGroup().addComponent(this.cbSunwardSpire)).addGroup(layout.createParallelGroup().addComponent(this.cbTombOfTheEldritchSun)).addGroup(layout.createParallelGroup().addComponent(this.cbTombOfTheHeretic)).addGroup(layout.createParallelGroup().addComponent(this.cbSecretQuest));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbShatteredRealms, this.cbTempleOfOsyr);
        layout.linkSize(0, this.cbShatteredRealms, this.cbKorvanOasis);
        layout.linkSize(0, this.cbShatteredRealms, this.cbCairanDocks);
        layout.linkSize(0, this.cbShatteredRealms, this.cbSandblownRuins);
        layout.linkSize(0, this.cbShatteredRealms, this.cbSanctuaryOfHorran);
        layout.linkSize(0, this.cbShatteredRealms, this.cbRuinsOfAbyd);
        layout.linkSize(0, this.cbShatteredRealms, this.cbValleyOfTheChosen);
        layout.linkSize(0, this.cbShatteredRealms, this.cbSunwardSpire);
        layout.linkSize(0, this.cbShatteredRealms, this.cbTombOfTheEldritchSun);
        layout.linkSize(0, this.cbShatteredRealms, this.cbTombOfTheHeretic);
        layout.linkSize(0, this.cbShatteredRealms, this.cbSecretQuest);
        layout.linkSize(1, this.cbShatteredRealms, this.cbTempleOfOsyr);
        layout.linkSize(1, this.cbShatteredRealms, this.cbKorvanOasis);
        layout.linkSize(1, this.cbShatteredRealms, this.cbCairanDocks);
        layout.linkSize(1, this.cbShatteredRealms, this.cbSandblownRuins);
        layout.linkSize(1, this.cbShatteredRealms, this.cbSanctuaryOfHorran);
        layout.linkSize(1, this.cbShatteredRealms, this.cbRuinsOfAbyd);
        layout.linkSize(1, this.cbShatteredRealms, this.cbValleyOfTheChosen);
        layout.linkSize(1, this.cbShatteredRealms, this.cbSunwardSpire);
        layout.linkSize(1, this.cbShatteredRealms, this.cbTombOfTheEldritchSun);
        layout.linkSize(1, this.cbShatteredRealms, this.cbTombOfTheHeretic);
        layout.linkSize(1, this.cbShatteredRealms, this.cbSecretQuest);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_ACT6"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbShatteredRealms == null) {
            (this.cbShatteredRealms = new JCheckBox()).setEnabled(false);
            this.cbShatteredRealms.addActionListener(new ChangeActionListener());
        }
        this.cbShatteredRealms.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_SHATTERED_REALMS"));
        this.cbShatteredRealms.setFont(fntCheck);
        if (this.cbTempleOfOsyr == null) {
            (this.cbTempleOfOsyr = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTempleOfOsyr.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_TEMPLE_OSYR"));
        this.cbTempleOfOsyr.setFont(fntCheck);
        if (this.cbKorvanOasis == null) {
            (this.cbKorvanOasis = new JCheckBox()).setEnabled(false);
            this.cbKorvanOasis.addActionListener(new ChangeActionListener());
        }
        this.cbKorvanOasis.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_KORVAN_OASIS"));
        this.cbKorvanOasis.setFont(fntCheck);
        if (this.cbCairanDocks == null) {
            (this.cbCairanDocks = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCairanDocks.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_CAIRAN_DOCKS"));
        this.cbCairanDocks.setFont(fntCheck);
        if (this.cbSandblownRuins == null) {
            (this.cbSandblownRuins = new JCheckBox()).setEnabled(false);
            this.cbSandblownRuins.addActionListener(new ChangeActionListener());
        }
        this.cbSandblownRuins.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_SANDBLOWN_RUINS"));
        this.cbSandblownRuins.setFont(fntCheck);
        if (this.cbSanctuaryOfHorran == null) {
            (this.cbSanctuaryOfHorran = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbSanctuaryOfHorran.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_SANCTUARY_HORRAN"));
        this.cbSanctuaryOfHorran.setFont(fntCheck);
        if (this.cbRuinsOfAbyd == null) {
            (this.cbRuinsOfAbyd = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbRuinsOfAbyd.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_RUINS_ABYD"));
        this.cbRuinsOfAbyd.setFont(fntCheck);
        if (this.cbValleyOfTheChosen == null) {
            (this.cbValleyOfTheChosen = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbValleyOfTheChosen.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_VALLEY_CHOSEN"));
        this.cbValleyOfTheChosen.setFont(fntCheck);
        if (this.cbSunwardSpire == null) {
            (this.cbSunwardSpire = new JCheckBox()).setEnabled(false);
            this.cbSunwardSpire.addActionListener(new ChangeActionListener());
        }
        this.cbSunwardSpire.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_SUNWARD_SPIRE"));
        this.cbSunwardSpire.setFont(fntCheck);
        if (this.cbTombOfTheEldritchSun == null) {
            (this.cbTombOfTheEldritchSun = new JCheckBox()).setEnabled(false);
            this.cbTombOfTheEldritchSun.addActionListener(new ChangeActionListener());
        }
        this.cbTombOfTheEldritchSun.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_ELDRITCH_SUN"));
        this.cbTombOfTheEldritchSun.setFont(fntCheck);
        if (this.cbTombOfTheHeretic == null) {
            (this.cbTombOfTheHeretic = new JCheckBox()).setEnabled(false);
            this.cbTombOfTheHeretic.addActionListener(new ChangeActionListener());
        }
        this.cbTombOfTheHeretic.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_TOMB_HERETIC"));
        this.cbTombOfTheHeretic.setFont(fntCheck);
        if (this.cbSecretQuest == null) {
            (this.cbSecretQuest = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbSecretQuest.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_06_SECRET_QUEST"));
        this.cbSecretQuest.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbShatteredRealms.setSelected(selected);
        this.cbTempleOfOsyr.setSelected(selected);
        this.cbKorvanOasis.setSelected(selected);
        this.cbCairanDocks.setSelected(selected);
        this.cbSandblownRuins.setSelected(selected);
        this.cbSanctuaryOfHorran.setSelected(selected);
        this.cbRuinsOfAbyd.setSelected(selected);
        this.cbValleyOfTheChosen.setSelected(selected);
        this.cbSunwardSpire.setSelected(selected);
        this.cbTombOfTheEldritchSun.setSelected(selected);
        this.cbSecretQuest.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getShrineList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbShatteredRealms.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_SHATTERED_REALMS);
        }
        if (this.cbTempleOfOsyr.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_TEMPLE_OSYR);
        }
        if (this.cbKorvanOasis.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_KORVAN_OASIS);
        }
        if (this.cbCairanDocks.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_CAIRAN_DOCKS);
        }
        if (this.cbSandblownRuins.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_SANDBLOWN_RUINS);
        }
        if (this.cbSanctuaryOfHorran.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_SANCTUARY_HORRAN);
        }
        if (this.cbRuinsOfAbyd.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_RUINS_ABYD);
        }
        if (this.cbValleyOfTheChosen.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_VALLEY_CHOSEN);
        }
        if (this.cbSunwardSpire.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_SUNWARD_SPIRE);
        }
        if (this.cbTombOfTheEldritchSun.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_ELDRITCH_SUN);
        }
        if (this.cbTombOfTheHeretic.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_LOST_OASIS);
        }
        if (this.cbSecretQuest.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_SECRET_QUEST);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        DBShrine.Info info = null;
        info = DBShrine.getShrineInfo("devotionshrineg09.dbr", this.difficulty);
        this.cbShatteredRealms.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg01.dbr", this.difficulty);
        this.cbTempleOfOsyr.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg08.dbr", this.difficulty);
        this.cbKorvanOasis.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg02.dbr", this.difficulty);
        this.cbCairanDocks.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg10.dbr", this.difficulty);
        this.cbSandblownRuins.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg07.dbr", this.difficulty);
        this.cbSanctuaryOfHorran.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg03.dbr", this.difficulty);
        this.cbRuinsOfAbyd.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg04.dbr", this.difficulty);
        this.cbValleyOfTheChosen.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg06.dbr", this.difficulty);
        this.cbSunwardSpire.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg05.dbr", this.difficulty);
        this.cbTombOfTheEldritchSun.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg11.dbr", this.difficulty);
        this.cbTombOfTheHeretic.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrines04.dbr", this.difficulty);
        this.cbSecretQuest.setEnabled(info.active && this.difficulty == 2);
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRestoredShrinesList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_SHATTERED_REALMS)) {
                this.cbShatteredRealms.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_TEMPLE_OSYR)) {
                this.cbTempleOfOsyr.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_KORVAN_OASIS)) {
                this.cbKorvanOasis.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_CAIRAN_DOCKS)) {
                this.cbCairanDocks.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_SANDBLOWN_RUINS)) {
                this.cbSandblownRuins.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_SANCTUARY_HORRAN)) {
                this.cbSanctuaryOfHorran.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_RUINS_ABYD)) {
                this.cbRuinsOfAbyd.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_VALLEY_CHOSEN)) {
                this.cbValleyOfTheChosen.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_SUNWARD_SPIRE)) {
                this.cbSunwardSpire.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_ELDRITCH_SUN)) {
                this.cbTombOfTheEldritchSun.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_LOST_OASIS)) {
                this.cbTombOfTheHeretic.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_06_SECRET_QUEST)) {
                this.cbSecretQuest.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharShrineAct6Pane.this.changed = true;
        }
    }
}
